package io.github.siltal.examsystembackend.entity;

import io.github.siltal.examsystembackend.converter.QuestionListConverter;
import io.github.siltal.examsystembackend.dto.ExamHistoryFetchRequest;
import io.github.siltal.examsystembackend.dto.ExamHistoryQO;
import io.github.siltal.examsystembackend.dto.ExamHistorySubmitRequest;
import jakarta.persistence.*; // 或者 javax.persistence.*
import java.time.Instant;
import java.util.List;

@Entity
@Table(name = "t_exam_history")
public class ExamHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id", nullable = false)
    private Long userId;

    @Column(name = "score")
    private Long score;

    // 这是关键部分
    // 1. @Convert 会告诉 JPA 使用我们自定义的转换器
    // 2. @Column(columnDefinition = "TEXT") 确保数据库字段类型足够大以存储JSON字符串
    //    如果你的数据库是 PostgreSQL 或 MySQL 8+，也可以用 "JSON"
    @Convert(converter = QuestionListConverter.class)
    @Column(name = "questions", columnDefinition = "TEXT")
    private List<ExamHistoryQO> questions;

    @Column(name = "created_at", nullable = false)
    private Instant createdAt = Instant.now();

    // JPA 需要一个无参构造函数
    public ExamHistory() {
    }
    public ExamHistory(ExamHistorySubmitRequest submit) {
        this.userId = submit.id();
        this.score = submit.score();
        this.questions = submit.questions();
    }


    // --- Getters and Setters ---

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getScore() {
        return score;
    }

    public void setScore(Long score) {
        this.score = score;
    }

    public List<ExamHistoryQO> getQuestions() {
        return questions;
    }

    public void setQuestions(List<ExamHistoryQO> questions) {
        this.questions = questions;
    }

    public Instant getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Instant createdAt) {
        this.createdAt = createdAt;
    }
}